//系统用户
module.exports = app => {
    const DataTypes = app.Sequelize;
    const SysUser = app.model.define('SysUser', {
        id: {
            type: DataTypes.INTEGER(11).UNSIGNED,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
        },
        shop_id: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            defaultValue: null,
            comment: '商家ID，预留SASS功能'
        },
        is_sys_user: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: '0',
            comment: '是否为系统用户'
        },
        username: {
            type: DataTypes.STRING(45),
            allowNull: false,
            defaultValue: '',
        },
        mobile: {
            type: DataTypes.STRING(15),
            allowNull: false,
            defaultValue: '',
        },
        password: {
            type: DataTypes.STRING(255),
            allowNull: false,
            defaultValue: '',
        },
        password_salt: {
            type: DataTypes.STRING(255),
            allowNull: false,
            defaultValue: '',
        },
        operation_password: {
            type: DataTypes.STRING(255),
            allowNull: false,
            defaultValue: '',
        },
        operation_password_salt: {
            type: DataTypes.STRING(255),
            allowNull: false,
            defaultValue: '',
        },
        avatar: {
            type: DataTypes.STRING(255),
            allowNull: false,
            defaultValue: '',
        },
        last_login_ip: {
            type: DataTypes.STRING(60),
            allowNull: false,
            defaultValue: '',
            comment: '最近登陆IP'
        },
        last_login_time: {
            type: DataTypes.INTEGER(11).UNSIGNED,
            allowNull: false,
            defaultValue: '0',
            comment: '最近登陆时间'
        },
        joined_time: {
            type: DataTypes.INTEGER(11).UNSIGNED,
            allowNull: false,
            defaultValue: '0',
            comment: '入职时间'
        },
        quit_time: {
            type: DataTypes.INTEGER(11).UNSIGNED,
            allowNull: false,
            defaultValue: '0',
            comment: '离职时间'
        },
        userid: {
            type: DataTypes.STRING(30),
            allowNull: false,
            defaultValue: '',
        },
        position: {
            type: DataTypes.STRING(30),
            allowNull: false,
            defaultValue: '',
        },
        status: {
            type: DataTypes.INTEGER(1).UNSIGNED,
            allowNull: false,
            defaultValue: '0',
            comment: '状态（0=>正常, 1=>锁定,2=>离职）'
        },
        remark: {
            type: DataTypes.STRING(60),
            allowNull: false,
            defaultValue: '',
            comment: '备注'
        },
        is_delete: {
            type: DataTypes.INTEGER(1).UNSIGNED,
            allowNull: false,
            defaultValue: '0',
            comment: '是否删除，0->存在，1->删除'
        },
    }, {
        paranoid: true,
        tableName: 'sys_user',
    });
    SysUser.associate = function () {

        SysUser.belongsTo(app.model.Shop, {
            as: "Shop",
            foreignKey: 'shop_id',
            constraints: false
        });

        SysUser.hasMany(app.model.SysUserRole, {
            as: 'UserRoles',
            foreignKey: 'sys_user_id',
            constraints: false,
        });
    };

    return SysUser;
};
